//
//  ManyListenListViewController.m
//  NewMusic
//
//  Created by dllo on 15/11/15.
//  Copyright © 2015年 Going. All rights reserved.
//

#import "ManyListenListViewController.h"
#import "CustomerNetworking.h"
#import "ListenDataSongsModel.h"
#import "ManyListenListTableViewCell.h"
#import "ManyListenHeaderView.h"
#import "NavigationBarBackImageView.h"
#import "MusicType.h"
#import "PlayerViewController.h"
#import "SongsAuditionListModel.h"
@interface ManyListenListViewController ()<UITableViewDataSource, UITableViewDelegate, UIScrollViewDelegate>
@property(nonatomic, retain) NSMutableArray *songsArray;
@property(nonatomic, retain) UITableView *tableView;
@property(nonatomic, copy) NSString *picString;
@property(nonatomic, assign) CGFloat contentOffSety;
@property(nonatomic, retain) ManyListenHeaderView *headerView;
@property(nonatomic, retain) UINavigationBar *bar;
@property(nonatomic, retain) NavigationBarBackImageView *barBackImgeView;
@end

@implementation ManyListenListViewController
- (void)dealloc {
    [_bar release];
    [_barBackImgeView release];
    [_headerView release];
    [_picString release];
    [_tableView release];
    [_songsArray release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.songsArray = [NSMutableArray array];
    //创建tableView
    [self createTableView];
    //获取数据
    [self loadData:@"0" version:@"0"];
    //创建自定义navigationbar
    [self createTabbar];
}
#pragma mark 创建自定义navigationbar
- (void)createTabbar {
    self.bar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 375, 64)];
    [self.view addSubview:self.bar];
    //navigationbar背景图片
    self.barBackImgeView = [[NavigationBarBackImageView alloc]initWithFrame:CGRectMake(0, 0, self.bar.frame.size.width, self.bar.frame.size.height)];
    self.barBackImgeView.titleString = @"大家在听";
    _barBackImgeView.alpha = 0;
    [_bar addSubview:_barBackImgeView];
    [_barBackImgeView release];
    
    self.bar.tintColor = [UIColor whiteColor];
    [self.bar setBackgroundImage:[UIImage imageNamed:@"nav.jpg"] forBarMetrics:UIBarMetricsCompact];
    [[self findHairlineImageViewUnder:self.bar] setHidden:YES];
    UIBarButtonItem *itemBar = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon_fanhui"] style:UIBarButtonItemStylePlain target:self action:@selector(retutnAction:)];
    UINavigationItem *item = [[UINavigationItem alloc] init];
    [self.bar pushNavigationItem:item animated:YES];
    [item setLeftBarButtonItem:itemBar];
    [item release];
    [_bar release];
}
- (void)retutnAction:(UIButton *)button {
    [self.navigationController popViewControllerAnimated:YES];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}
- (UIImageView *)findHairlineImageViewUnder:(UIView *)view {
    if ([view isKindOfClass:UIImageView.class] && view.bounds.size.height <= 1.0) {
        return (UIImageView *)view;
    }
    for (UIView *subview in view.subviews) {
        UIImageView *imageView = [self findHairlineImageViewUnder:subview];
        if (imageView) {
            return imageView;
        }
    }
    return nil;
}

#pragma mark 获取数据
- (void)loadData:(NSString *)random version:(NSString *)version {
    NSLog(@"http://api.favorite.ttpod.com/favorite/song/plaza?random=%@&version=%@&app=ttpod&v=v8.1.1.2015110616", random, version);
    [CustomerNetworking customerConnectWithURL:[NSString stringWithFormat:@"http://api.favorite.ttpod.com/favorite/song/plaza?random=%@&version=%@&app=ttpod&v=v8.1.1.2015110616", random, version] andParameters:nil andBlock:^(id response) {
        NSArray *array = [[response valueForKey:@"data"] valueForKey:@"songs"];
        if (array.count != 0) {
            for (NSDictionary *dic in array) {
                ListenDataSongsModel *model = [[ListenDataSongsModel alloc]initWithDictionary:dic];
                [self.songsArray addObject:model];
                [model release];
            }
        }
        if ([[[[response valueForKey:@"data"] valueForKey:@"image"] valueForKey:@"pic"]  isKindOfClass:[NSNull class]]) {
            _headerView.picString = @"";
        }else {
       _headerView.picString =[[[response valueForKey:@"data"] valueForKey:@"image"] valueForKey:@"pic"];
        }
        
        [_tableView reloadData];
    }];
}
#pragma mark 创建tableView
- (void)createTableView {
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    self.contentOffSety = _tableView.contentOffset.y;
    _tableView.backgroundColor = [UIColor clearColor];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellEditingStyleNone;
    //创建头视图
    self.headerView = [[ManyListenHeaderView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 250)];
    _tableView.tableHeaderView = _headerView;
    //全部播放的点击事件
    [_headerView.listView.showButton addTarget:self action:@selector(showButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    //换一批的点击事件
    [_headerView.listView.changeButton addTarget:self action:@selector(changeButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    //注册单元格
    [_tableView registerClass:[ManyListenListTableViewCell class] forCellReuseIdentifier:@"ManyListenListTableViewCellIdentify"];
    [self.view addSubview:_tableView];
    [_tableView release];
}
#pragma mark cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ManyListenListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ManyListenListTableViewCellIdentify"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (_songsArray.count != 0) {
        cell.dataSongsModel = _songsArray[indexPath.row];
    }
    cell.queueString = [NSString stringWithFormat:@"%ld", indexPath.row + 1];
    cell.backgroundColor = [UIColor clearColor];
    return  cell;
}
#pragma mark cell个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (_songsArray.count != 0) {
        return _songsArray.count;
    }else {
        return 0;
    }
}
#pragma mark cell大小
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return self.view.frame.size.height / 10;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
PlayerViewController *player = [PlayerViewController sharePlayerViewController];
    NSMutableArray *arr = [NSMutableArray array];
        ListenDataSongsModel *song = self.songsArray[indexPath.row];
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        [dic setValue:song.albumName forKey:@"album"];// 专辑
        [dic setValue:song.name forKey:@"name"]; // 歌名
        NSMutableArray *array = [NSMutableArray array];
        for (SongsAuditionListModel *model in song.auditionList) {
            MusicType *type = [[MusicType alloc]init];
            type.url = model.urlString;
            type.typeDescription = model.typeDescription;
            [array addObject:type];
        }
        [dic setValue:array forKey:@"music"]; // 歌曲,model用musicType
        [dic setValue:song.singerName forKey:@"songerName"];// 歌手名
        [dic setValue:song.albumId forKey:@"id"]; // id
        [arr addObject:dic];
    player.musicArr = arr;
    [player MusicStart:0];
}
#pragma mark scrollView的代理方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView.contentOffset.y < -20)
    {
        CGFloat factor = ABS(scrollView.contentOffset.y) + 250 * KPropotionHeight - 20;
        CGRect f = CGRectMake(-([[UIScreen mainScreen] bounds].size.width*(ABS(scrollView.contentOffset.y) + 250 - 20)/250-[[UIScreen mainScreen] bounds].size.width)/2, -ABS(scrollView.contentOffset.y)+ 20, [[UIScreen mainScreen] bounds].size.width*(ABS(scrollView.contentOffset.y) + 250 - 20)/250, factor);
        self.headerView.picImage.frame = f;
    } else {
        CGRect f = self.headerView.frame;
        f.origin.y = 0;
        self.headerView.frame = f;
        self.headerView.picImage.frame = CGRectMake(0, f.origin.y, [[UIScreen mainScreen] bounds].size.width, 250 * KPropotionHeight);
    }
    _headerView.listView.backView.alpha = _tableView.contentOffset.y / 100;
    if (_headerView.listView.backView.alpha > 1) {
        _headerView.listView.backView.alpha =  1;
    }else if (_headerView.listView.backView.alpha < 0.7) {
        _headerView.listView.backView.alpha =  0.7;
    }
    _barBackImgeView.alpha = _tableView.contentOffset.y / 100;
    if (_tableView.contentOffset.y > 115 && _tableView.contentOffset.y > _contentOffSety) {
        _tableView.frame = CGRectMake(0, -20, _tableView.frame.size.width, _tableView.frame.size.height);
        _headerView.listView.frame = CGRectMake(0, 64, _headerView.listView.frame.size.width, _headerView.listView.frame.size.height);
        [self.view addSubview:_headerView.listView];
        }else if ( self.tableView.contentOffset.y < 115 && _tableView.contentOffset.y < _contentOffSety){
          _tableView.frame = CGRectMake(0, -20, _tableView.frame.size.width, _tableView.frame.size.height);
            _headerView.listView.frame = CGRectMake(0, _tableView.tableHeaderView.frame.size.height - _headerView.listView.frame.size.height, _headerView.listView.frame.size.width, _headerView.listView.frame.size.height);
            [_tableView.tableHeaderView addSubview:_headerView.listView];
        }
    self.contentOffSety = _tableView.contentOffset.y;
}
#pragma mark 全部播放的点击事件
- (void)showButtonAction:(UIButton *)button {
    PlayerViewController *player = [PlayerViewController sharePlayerViewController];
    NSMutableArray *arr = [NSMutableArray array];
    for (NSInteger i = 0; i < self.songsArray.count; i++)
    {
        ListenDataSongsModel *song = self.songsArray[i];
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        [dic setValue:song.albumName forKey:@"album"];// 专辑
        [dic setValue:song.name forKey:@"name"]; // 歌名
        NSMutableArray *array = [NSMutableArray array];
        for (SongsAuditionListModel *model in song.auditionList) {
            MusicType *type = [[MusicType alloc]init];
            type.url = model.urlString;
            type.typeDescription = model.typeDescription;
            [array addObject:type];
        }
        [dic setValue:array forKey:@"music"]; // 歌曲,model用musicType
        [dic setValue:song.singerName forKey:@"songerName"];// 歌手名
        [dic setValue:song.albumId forKey:@"id"]; // id
        [arr addObject:dic];
    }
    player.musicArr = arr;
    [player MusicStart:0];
}
#pragma mark 换一批的点击事件
- (void)changeButtonAction:(UIButton *)button {
    self.songsArray = [NSMutableArray array];
    [self loadData:@"1" version:@"402273"];
    [_tableView reloadData];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
